package com.vallis.asfc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.asfc.entity.Permission;
import com.vallis.asfc.entity.User;
import com.vallis.asfc.entity.UserPermission;
import com.vallis.asfc.service.PermissionService;
import com.vallis.asfc.service.UserPermissionService;
import com.vallis.asfc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-06-14
 */
@RestController
@CrossOrigin
@RequestMapping("/user-permission")
public class UserPermissionController {
@Autowired
    private UserPermissionService userPermissionService;
    @Autowired
    private PermissionService permissionService;
    @GetMapping("menuList")
    public Result getUserMenu(){
        int userid=1;
        List<UserPermission> list = userPermissionService.list(new QueryWrapper<UserPermission>().eq("user_id", userid).eq("permission_type", 1));
        List<Permission> menuList=new ArrayList<>();
        for (UserPermission userPermission : list) {
            Permission id = permissionService.getOne(new QueryWrapper<Permission>().eq("id", userPermission.getPermissionId()));
            id.setChildrenList(permissionService.list(new QueryWrapper<Permission>().eq("parent_id", userPermission.getPermissionId())));
            menuList.add(id);
        }
        System.out.println("获取菜单成功！"+menuList.size());
        return new Result(200,"获取成功",menuList);

    }

    /**
     * 获取该用户所有的权限
     * @param userId
     * @return
     */
    @GetMapping("getThisUserPermission")
    public Result getThisUserPermission(Integer userId){
        System.out.println("获取到的userID是"+userId);
        //获取该用户的所有的权限
        List<UserPermission> userRolePermission = userPermissionService.list(new QueryWrapper<UserPermission>().eq("user_id", userId));


        List<Permission> thisUserPermissions = getUserPermission(userRolePermission);

    List<Permission> result=new ArrayList<>();
    thisUserPermissions.forEach(item->{
        System.out.println(item);
        if(null!=item) {
            if (item.getParentId() == 0) {
                result.add(item);
        
            }
        }

    });

    result.forEach(item->{
            item.setChildrenList( iterateMenus(thisUserPermissions,item));
       ;

    });

        System.out.println("该用户的的所有菜单大小为："+result.size());
    return new Result(200,"成功",result);

    }





    public List<Permission> iterateMenus(List<Permission> thisUserPermissions,Permission permission){

        List<Permission> result =new ArrayList<>();
        thisUserPermissions.forEach(item->{
            if (null != item) {
                if (permission.getId().equals(item.getParentId())) {
                    List<Permission> iterateMenu = iterateMenus(thisUserPermissions, item);
                    item.setChildrenList(iterateMenu);
                    result.add(item);
            
            
                }
        
            }
        });

        return result;
    }

    public List<Permission> getUserPermission(List<UserPermission> userPermissions){
        List<Permission>  userFirstPermission=new ArrayList<>();
        userPermissions.forEach(item->{
            userFirstPermission.add(  permissionService.getOne(new QueryWrapper<Permission>().eq("id",item.getPermissionId())));

        });

        return userFirstPermission;
    }





}
